﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Psl.Controls;

namespace Stl.Chat.Components
{
    public class DesignAction
    {
        public static char Delimiter { get { return ':'; } }
        public static string Graphics { get { return "graphics"; } }
        protected bool initialized = false;
    }

    class DesignDrop : DesignAction
    {
        public string TypeName { get; protected set; }
        public string Name { get; protected set; }
        public int X { get; protected set; }
        public int Y { get; protected set; }
        public int Width { get; protected set; }
        public int Height { get; protected set; }
        public Control Control { get; protected set; }

        public static string Serialize(Control control)
        {
            Type t = control.GetType();
            return "graphics:ControlAdd:" + t.AssemblyQualifiedName + ":" + control.Name + ":" + control.Location.X 
                + ":" + control.Location.Y + ":" + control.Size.Width + ":" + control.Size.Height;
        }

        public bool Deserialize(string[] strs)
        {
            if (strs.Length != 8)
                return false;
            TypeName = strs[2];
            try
            {
                Type type = Type.GetType(TypeName);
                if (type == null)
                    return false;
                Control = (Control)type.GetConstructor(new Type[0]).Invoke(new Object[0]);
                Name = strs[3];
                X = int.Parse(strs[4]);
                Y = int.Parse(strs[5]);
                Width = int.Parse(strs[6]);
                Height = int.Parse(strs[7]);

                Control.Name = Name;
                Control.SetBounds(X, Y, Width, Height);
                initialized = true;
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }

        public bool Action(DesignFrame frame)
        {
            if (!initialized)
                return false;
            frame.Controls.Add(Control);
            return true;
        }
    }

    class DesignRemove : DesignAction
    {
        public string Name { get; protected set; }

        public static string Serialize(Control control)
        {
            return "graphics:ControlRemove:" + control.Name;
        }

        public bool Deserialize(string[] strs)
        {
            Name = strs[2];
            initialized = true;
            return true;
        }

        public bool Action(DesignFrame frame)
        {
            if (!initialized)
                return false;
            frame.Controls.Remove(frame.Controls[Name]);
            return true;
        }
    }

    class DesignSelect : DesignAction
    {
        public string Name { get; protected set; }

        public static string Serialize(Control control)
        {
            return "graphics:ControlSelect:" + control.Name;
        }

        public bool Deserialize(string[] strs)
        {
            Name = strs[2];
            initialized = true;
            return true;
        }

        public bool Action(DesignFrame frame)
        {
            if (!initialized)
                return false;
            if (frame.SelectedControl == null
                || !frame.SelectedControl.Name.Equals(Name))
            {
                frame.SelectedControl = frame.Controls[Name];
                /*
                foreach(Control c in frame.Controls)
                    if (c.Name.Equals(Name))
                    {
                        frame.SelectedControl = c;
                        return true;
                    }
                */
            }
            return false;
        }
    }

    class DesignDrag : DesignAction
    {
        public string Name { get; protected set; }
        public int X { get; protected set; }
        public int Y { get; protected set; }
        public int Width { get; protected set; }
        public int Height { get; protected set; }

        public static string Serialize(Control control)
        {
            return "graphics:ControlDrag:" + control.Name + ":" + control.Location.X
                + ":" + control.Location.Y + ":" + control.Size.Width + ":" + control.Size.Height;
        }

        public bool Deserialize(string[] strs)
        {
            if (strs.Length != 7)
                return false;
            try
            {
                Name = strs[2];
                X = int.Parse(strs[3]);
                Y = int.Parse(strs[4]);
                Width = int.Parse(strs[5]);
                Height = int.Parse(strs[6]);
                initialized = true;
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }

        public bool Action(DesignFrame frame)
        {
            if (!initialized)
                return false;
            Control c = frame.Controls[Name];
            if (c == null)
                return false;
            c.SetBounds(X, Y, Width, Height);
            frame.Invalidate();
            return true;
        }
    }
}
